/**
 * Created by Administrator on 2017/4/12.
 */


var GlobalConfig = require('../../config/config.json');
var redis = require("redis");

module.exports = {
    /**
     * 加载Redis
     * @param callback(err, client)
     */
    loadDB: function(callback) {
        if (!GlobalConfig.redis) {
            callback(); return;
        }

        var redisConfig = {
            "host": GlobalConfig.redis.host,
            "port": GlobalConfig.redis.port
        }
        var client = redis.createClient(redisConfig);

        async.series([
            function(cb) {
                client.select(GlobalConfig.redis.index, cb);
            },
        ], function(err) {
            if (err) {
                callback(err); return;
            }

            callback(null, client);

            client.on('error', function(e){
                logger.error("redis exception: %j", e);
            });
        });
    }
}